<!--添加审批节点-->
<template>
  <edit-dialog :centerDialogVisible="visible" tilte="新增流程节点" width="600" @Cancel="handleCancel" @Submit="handleConfirmAdd" :defaultButton="false">
    <template #content>
      <el-form :model="form" ref="formRef" :rules="rules" class="add-flow-node" label-width="100">
        <div class="header">
          <div class="header-block"></div>
          <div class="header-title">节点类型</div>
        </div>
        <el-form-item label-width="30" prop="nodeType">
          <el-radio-group v-model="form.nodeType" @change="nodeTypeChange" disabled>
            <el-radio v-for="item in options.nodeTypeEnum" :label="item.value">{{ item.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <template v-if="form.nodeType === options.nodeTypeEnum[0].value">
          <div class="header">
            <div class="header-block"></div>
            <div class="header-title">选择预设节点</div>
          </div>
          <el-form-item label="选择节点" prop="nodeContent.state">
            <el-select v-model="form.nodeContent.state" style="width: 400px" @change="normalChange">
              <el-option v-for="item in options.approveNodeTypeEnum" :label="item.label" :value="item.value" :disabled="disabledInputNode.includes(item.value)"/>
            </el-select>
          </el-form-item>
        </template>

        <template v-else>
          <div class="header">
            <div class="header-block"></div>
            <div class="header-title">是否自定义节点</div>
          </div>
          <el-form-item prop="isCustom">
            <el-radio-group v-model="form.isCustom" style="width: 400px" @change="customChange">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>

          <template v-if="!form.isCustom">
            <div class="header">
              <div class="header-block"></div>
              <div class="header-title">选择常用节点</div>
            </div>
            <el-form-item label="选择节点" prop="id" @change="">
              <el-select v-model="form.id" style="width: 400px" clearable @change="commonChange">
                <el-option v-for="item in options.commonProcessNodes" :label="item.nodeName" :value="item.id" :disabled="disabledCommonNode.includes(item.id)"/>
              </el-select>
            </el-form-item>
          </template>

          <template v-else>
            <div class="header">
              <div class="header-block"></div>
              <div class="header-title">设置节点规则</div>
            </div>
            <el-form-item label="节点名称" prop="nodeName">
              <el-input v-model="form.nodeName" style="width: 400px"/>
            </el-form-item>
            <el-form-item label="审批类型" prop="approvalType">
              <el-cascader v-model="form.approvalType" :show-all-levels="false" :options="options.approvalTypeTree"
                           style="width: 400px"/>
            </el-form-item>
            <el-form-item label="设为常用节点" prop="isCommonly">
              <el-radio-group v-model="form.isCommonly">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </template>
        </template>
      </el-form>
    </template>
    <template #custom>
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleConfirmAdd">确 定</el-button>
    </template>
  </edit-dialog>
</template>

<script>
import EditDialog from "@/components/editDialog/index.vue";

export default {
  name: "AddFlowNode",
  props: {
    allNode: {
      type: Array,
      default: () => []
    },
  },
  components: {EditDialog},
  data() {
    return {
      visible: false,
      form: {
        nodeContent: {
          state: undefined
        },
        isCommonly: 0,
      },
      disabledInputNode: [],
      disabledCommonNode: [],
      rules: {
        nodeType: [
          { required: true, message: '请选择', trigger: ['blur', 'change'] },
        ],
        'nodeContent.state': [
          { required: true, message: '请选择', trigger: ['blur', 'change'] },
        ],
        isCustom: [
          { required: true, message: '请选择', trigger: ['blur', 'change'] },
        ],
        id: [
          { required: true, message: '请选择', trigger: ['blur', 'change'] },
        ],
        nodeName: [
          { required: true, message: '请输入', trigger: ['blur', 'change'] },
        ],
        approvalType: [
          { required: true, message: '请选择', trigger: ['blur', 'change'] },
        ],
      },
    }
  },
  inject: ["options"],
  methods: {
    open({resolve}) {
      this.form.id = undefined
      this.form.nodeContent.state = undefined
      this.form.isCustom = 0
      this.form.isCommonly = 0
      this.visible = true
      this.resolve = resolve

      let disabledInputNode = []
      let disabledCommonNode = []
      this.allNode.forEach(item => {
        if (item.nodeType === this.options.nodeTypeEnum[0].value) {
          disabledInputNode.push(item.nodeContent.state)
        }
        // if (item.nodeType === this.options.nodeTypeEnum[1].value && item.id) {
        //   disabledCommonNode.push(item.id)
        // }
      })
      this.disabledInputNode = disabledInputNode
      this.disabledCommonNode = disabledCommonNode
      let beforeNode = this.allNode[this.allNode.length - 1]
      if (beforeNode && beforeNode.nodeType === this.options.nodeTypeEnum[0].value) {
        this.form.nodeType =  this.options.nodeTypeEnum[1].value
      } else {
        this.form.nodeType =  this.options.nodeTypeEnum[0].value
      }
    },
    handleConfirmAdd() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          let {approvalType, ...more} = JSON.parse(JSON.stringify(this.form))
          this.resolve({...more, approvalType: approvalType ? approvalType[approvalType.length -1] : undefined})
          this.handleCancel()
        }
      })
    },
    handleCancel() {
      this.form.id = undefined
      this.$refs.formRef.resetFields()
      this.visible = false
    },
    nodeTypeChange() {
      this.form.approvalType = undefined
      this.form.nodeName = undefined
      this.form.isCommonly = undefined
      this.form.nodeContent.state = undefined
    },
    // 是否自定义节点接环
    customChange() {
      this.form.id = undefined
      this.form.approvalType = undefined
      this.form.nodeName = undefined
      this.form.isCommonly = undefined
      this.form.nodeContent.state = undefined
    },
    // 预设节点设置
    normalChange(val) {
      let find = this.options.approveNodeTypeEnum.find(item => item.value === val)
      this.form.nodeName = find.label
    },
    // 常用节点选择
    commonChange(val) {
      let find = this.options.commonProcessNodes.find(item => item.id === val)
      this.form.approvalType = find.approvalType
      this.form.nodeName = find.nodeName
    },
  }
}
</script>

<style lang="less" scoped>
.add-flow-node {
  overflow: auto;
  padding-right: 10px;

  .header {
    display: flex;
    align-items: center;
    margin-bottom: 15px;

    .header-block {
      height: 20px;
      width: 5px;
      background: #337ECA;
      margin-right: 10px;
    }

    .header-title {
      font-size: 16px;
      font-weight: bold;
    }
  }

}
</style>
